<!--
 * @Description: 基于 element-ui el-pagination 二次开发
 * @Author bianpengfei
 * @create 2021/4/26 23:05
 -->
<template>
  <div class="pagination__wrap">
    <el-pagination
      v-bind="$attrs"
      :current-page.sync="_page"
      :page-size.sync="_size"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-on="$listeners"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    total: {
      // 个数
      required: true,
      type: Number
    },
    page: {
      // 页码
      type: Number,
      default: 1
    },
    size: {
      // 当页 记录条数
      type: Number,
      default: 10
    },
    pageSizes: {
      // 每页显示大小 选项
      type: Array,
      default() {
        return [10, 20, 30, 50]
      }
    },
    layout: {
      // 组件布局，子组件名用逗号分隔
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    }
  },
  computed: {
    /**
     * 当前页
     */
    _page: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    /**
     * 当前页条数
     */
    _size: {
      get() {
        return this.size
      },
      set(val) {
        this.$emit('update:size', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this._page, size: val })
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, size: this._size })
    }
  }
}
</script>

<style scoped lang="scss">
.pagination__wrap {
  background: #fff;
  padding: 5px 16px;
  text-align: right;
}
</style>
